<template>
  <div class="view-fill">
    <div
      v-if="
        !revokeInfo.visible &&
        !modifyInfo.visible &&
        !purchOrderDetailInfo.visible &&
        !saleOrderDetailInfo.visible &&
        !receivingDetailInfo.visible &&
        !transferOrderDetailInfo.visible &&
        !consumeDetailInfo.visible &&
        !deliverDetailInfo.visible
      "
      class="app-container-flex"
    >
      <NewFullScreen
        v-model="showDialog"
        title="寄售消耗确认单详情"
        :btn-list="fullInfo.btnList"
        :needOperateAffair="true"
        :operateAffairData="affairTableInfo"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button v-if="canUpdate" @click="update">修改</el-button>
          <el-button v-if="canSubmit" @click="submit">提交</el-button>
          <el-button v-if="canRatify" @click="ratify">审核</el-button>
          <el-button v-if="canReview" @click="review">复核</el-button>
          <el-button v-if="canConfirm" @click="confirm">确认</el-button>
          <el-button v-if="canConfirmCancel" @click="confirmCancel"
            >取消确认</el-button
          >
          <el-button v-if="canReject" @click="reject">驳回</el-button>
          <el-button v-if="canRevoke" @click="revoke">撤回</el-button>
          <el-button v-if="canGoBack" @click="goBack">退回</el-button>
          <el-button v-if="canCancel" @click="cancel">取消</el-button>
          <PopoverPrint
            ref="popoverShow"
            v-model:popoverVisible="popoverVisible"
            :canPrint="
              infoList.data.status !== 1 &&
              $permission('consignConsumeConfirmOrder:print')
            "
            :templateArr="templateArr"
            @tempClick="tempClick"
            @print="print"
          ></PopoverPrint>
          <!-- <el-button
            v-if="
              infoList.data.status !== 1 &&
              $permission('consignConsumeConfirmOrder:print')
            "
            @click="print"
            >打印</el-button
          > -->
          <el-button
            v-if="
              infoList.data.status !== 1 &&
              $permission('consignConsumeConfirmOrder:exportExcel')
            "
            @click="exportExcel"
            >导出excel</el-button
          >
        </template>
        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <BasicInformation :info="infoList" labelWidth="100px">
              <template v-slot:basic-consumeOrderNo>
                <div v-if="infoList.data.consumeOrderNo">
                  <span
                    v-for="(item, index) in getOrderNoList(
                      infoList.data.consumeOrderNo
                    )"
                    :key="index"
                  >
                    <a
                      v-if="index <= 1"
                      class="slip-edit"
                      @click="
                        toSourceDetail(proxy, item, '', {
                          returnPath: '/stock/consignConsumeConfirmOrder',
                        })
                      "
                      >{{ item }}</a
                    >
                    <span
                      v-if="
                        index < 1 &&
                        getOrderNoList(infoList.data.consumeOrderNo).length > 1
                      "
                      >,</span
                    >
                  </span>
                  <el-dropdown
                    v-if="
                      getOrderNoList(infoList.data.consumeOrderNo).length > 2
                    "
                  >
                    <span class="el-dropdown-link slip-edit">
                      更多
                      <i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <template #dropdown>
                      <el-dropdown-menu>
                        <el-dropdown-item
                          v-for="(item, index) in getOrderNoList(
                            infoList.data.consumeOrderNo
                          )"
                          :key="index"
                          @click="
                            toSourceDetail(proxy, item, '', {
                              returnPath: '/stock/consignConsumeConfirmOrder',
                            })
                          "
                          >{{ item }}</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </template>
                  </el-dropdown>
                </div>
                <span v-else>-</span>
                <!-- <span class="slip-edit">
                  <span
                    v-for="(item, index) in getOrderNoList(
                      infoList.data.consumeOrderNo
                    )"
                    @click="
                      toSourceDetail(proxy, item, '', {
                        returnPath: '/stock/consignConsumeConfirmOrder',
                      })
                    "
                    >{{
                      item +
                      (getOrderNoList(infoList.data.consumeOrderNo).length ===
                      index + 1
                        ? ''
                        : ',')
                    }}</span
                  >
                </span>
                <span
                  v-if="
                    getOrderNoList(infoList.data.consumeOrderNo).length === 0
                  "
                  >-</span
                > -->
              </template>
            </BasicInformation>
          </div>
        </div>
        <div class="model-panel">
          <div class="model-panel-title btnList">
            <Tooltip :text="$t('detailInfo')"></Tooltip>
            <el-button
              v-if="!toSummaryFlag"
              style="margin-left: auto; margin-right: 10px"
              @click="checkSummaryFlag(true)"
              >汇总页</el-button
            >
            <el-button
              v-else
              style="margin-left: auto; margin-right: 10px"
              @click="checkSummaryFlag(false)"
              >消耗页</el-button
            >
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                v-if="toSummaryFlag"
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :summary="true"
                :check-box="false"
                :tab-index="true"
                :summary-field-list="tableInfo.summaryFieldList"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
              >
                <template v-slot:col-stockPrice="scope">
                  <span
                    :style="
                      scope.row.adjustPriceOrderDetailVOS
                        ? 'color: #f7c1c1;cursor: pointer'
                        : ''
                    "
                    >{{ $fn.TripartiteMethod(scope.row.stockPrice) }}</span
                  >
                </template>
                <template v-slot:col-price="scope">
                  <span
                    :style="
                      scope.row.adjustPriceOrderDetailVOS
                        ? 'color: #f7c1c1;cursor: pointer'
                        : ''
                    "
                    >{{ $fn.TripartiteMethod(scope.row.price) }}</span
                  >
                </template>
              </page-table>
              <page-table
                v-else
                :refresh="tableOrderInfo.refresh"
                :init-curpage="tableOrderInfo.initCurpage"
                ref="depotTable"
                :api="tableOrderInfo.api"
                v-model:data="tableOrderInfo.data"
                :check-box="false"
                :tab-index="true"
                :listen-height="false"
                :pager="false"
                :query="filterOrderInfo.query"
                :field-list="tableOrderInfo.fieldList"
              >
                <template v-slot:col-consumeOrderNo="scope">
                  <span
                    class="slip-edit"
                    @click="
                      toSourceDetail(
                        proxy,
                        scope.row.consumeOrderNo,
                        scope.row.sourceId,
                        { returnPath: '/stock/consignConsumeConfirmOrder' }
                      )
                    "
                    >{{ scope.row.consumeOrderNo || '-' }}</span
                  >
                </template>
              </page-table>
            </div>
          </div>
          <individual-details
            v-if="individualInfo.visible"
            v-model:show-dialog="individualInfo.visible"
            :query="individualInfo.query"
            :info="individualInfo.info"
            :consume-source-id="infoList.data.consumeSourceId"
            @reset="individualDetailsCallBack"
          ></individual-details>
        </div>
        <div class="model-panel">
          <div class="model-panel-body">
            <div class="flex_full_item">
              <el-tabs v-model="activeName">
                <el-tab-pane
                  class="model-panel-title"
                  label="寄售消耗确认关联表"
                  name="first"
                >
                  <!-- 表格 -->
                  <page-table
                    :refresh="consignOrderInfo.refresh"
                    :init-curpage="consignOrderInfo.initCurpage"
                    v-model:data="consignOrderInfo.data"
                    :pager="false"
                    :check-box="false"
                    :field-list="consignOrderInfo.fieldList"
                    :handle="consignOrderInfo.handle"
                  >
                    <template v-slot:col-relationOrderNo="scope">
                      <span
                        class="slip-edit"
                        @click="showDetailLink(scope.row)"
                        >{{ scope.row.relationOrderNo || '-' }}</span
                      >
                    </template>
                  </page-table>
                </el-tab-pane>
              </el-tabs>
            </div>
          </div>
        </div>
      </NewFullScreen>
    </div>
    <ConsumeRevoke
      v-if="revokeInfo.visible"
      :ids="revokeInfo.ids"
      v-model:visible="revokeInfo.visible"
      :order-id="revokeInfo.orderId"
      @returnModify="returnModify"
    />
    <Modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :orderId="modifyInfo.orderId"
      :title="modifyInfo.title"
      @returnModify="returnModify"
    />
    <purch-order-detail
      v-if="purchOrderDetailInfo.visible"
      v-model:visible="purchOrderDetailInfo.visible"
      :orderId="purchOrderDetailInfo.orderId"
      @returnBack="returnModify"
    />
    <saleOrderDetail
      v-if="saleOrderDetailInfo.visible"
      :id="saleOrderDetailInfo.id"
      v-model:visible="saleOrderDetailInfo.visible"
      @returnBack="returnModify"
    />
    <deliverDetail
      v-if="deliverDetailInfo.visible"
      :id="deliverDetailInfo.id"
      v-model:visible="deliverDetailInfo.visible"
      @returnDetail="returnModify"
    />
    <TransferOrderDetail
      v-if="transferOrderDetailInfo.visible"
      v-model:visible="transferOrderDetailInfo.visible"
      :table-row="transferOrderDetailInfo.data"
      @returnBack="returnModify"
    />
    <ReceivingDetail
      v-if="receivingDetailInfo.visible"
      v-model:visible="receivingDetailInfo.visible"
      :id="receivingDetailInfo.id"
      @returnModify="returnModify"
    />
    <ConsumeDetail
      v-if="consumeDetailInfo.visible"
      v-model:visible="consumeDetailInfo.visible"
      :id="consumeDetailInfo.id"
      @returnModify="returnModify"
    />
    <page-dialog
      v-show="adjustPriceDetailInfo.visible"
      :title="'调价单'"
      v-model:visible="adjustPriceDetailInfo.visible"
      :width="adjustPriceDetailInfo.width"
      :bt-loading="adjustPriceDetailInfo.btLoading"
      :bt-list="adjustPriceDetailInfo.btList"
    >
      <DialogTable
        v-model:data="adjustPriceDetailInfo.tableInfo.data"
        :field-list="adjustPriceDetailInfo.tableInfo.fieldList"
        :highlight-row="false"
        :check-box="false"
        :pager="false"
        :tabIndex="true"
      ></DialogTable>
    </page-dialog>
    <!-- 确认路径弹窗 -->
    <page-dialog
      v-if="dialogInfoPath.visible"
      :title="dialogInfoPath.title"
      v-model:visible="dialogInfoPath.visible"
      :width="dialogInfoPath.width"
      :bt-loading="dialogInfoPath.btLoading"
      :bt-list="dialogInfoPath.btList"
      @reset="close"
    >
      <dialog-table
        v-model:data="dialogTableInfo.data"
        :field-list="dialogTableInfo.fieldList"
        :highlight-row="true"
        :tab-index="true"
        :check-box="false"
        :pager="false"
        @handleEvent="handleDialogEvent"
      >
      </dialog-table>
    </page-dialog>
  </div>
</template>

<script setup name="consignConsumeConfirmOrderDetail">
import utils from '@/common/js/utils'
import { getCustomizeSetting } from '@/api/customize'
import {
  excelDetailConsignConsumeConfirmOrder,
  reviewConsignConsumeConfirmOrder,
  backConsignConsumeConfirmOrder,
  cancelConsignConsumeConfirmOrder,
  confirmConsignConsumeConfirmOrder,
  confirmCancelConsignConsumeConfirmOrder,
  rejectConsignConsumeConfirmOrder,
  revokeConsignConsumeConfirmOrder,
  submitConsignConsumeConfirmOrder,
  ratifyConsignConsumeConfirmOrder,
  getConsignConsumeConfirmDetailsCustomizeListAll,
  getConsignConsumeConfirmOrderById,
  printDetailConsignConsumeConfirmOrder,
  getConfirmPath,
  confirmByPath,
} from '@/api/stock.js'
import { orderAffairAll } from '@/api/affair'
import { getConsignOrderAll } from '@/api/mapping'
import { fatchTemplateByCode } from '@/api/base'
import { doPrint } from '@/utils/exportUtil.js'
import { selectByCodeAndDept } from '@/api/report.js'
import { updatePrintCount } from '@/api/mapping'
import { toSourceDetail } from '@/common/js/base.js'
import { defineAsyncComponent } from 'vue'

const Modify = defineAsyncComponent(() => import('./modify'))
const ConsumeRevoke = defineAsyncComponent(() =>
  import('../consumeRevoke/index')
)
const IndividualDetails = defineAsyncComponent(() =>
  import('./IndividualDetails')
)
const PurchOrderDetail = defineAsyncComponent(() =>
  import('@/views/purch/order/detail')
)
const saleOrderDetail = defineAsyncComponent(() =>
  import('@/views/sale/saleOrder/detail')
)
const deliverDetail = defineAsyncComponent(() =>
  import('@/views/deliver/Invoice/detail')
)
const ReceivingDetail = defineAsyncComponent(() =>
  import('@/views/stock/receiving/detail')
)
const TransferOrderDetail = defineAsyncComponent(() =>
  import('@/views/stock/transfer/detail')
)
const ConsumeDetail = defineAsyncComponent(() =>
  import('@/views/stock/consumeOrder/detail')
)
const AdjustPriceDetail = defineAsyncComponent(() =>
  import('@/views/collaborativeApproval/adjustPrice/detail')
)

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps(['visible', 'id', 'pageType'])

const toSummaryFlag = ref(true)
const activeName = ref('first')
const infoList = ref({
  data: {},
  fieldList: [
    { label: '单号', value: 'orderNo', type: 'text' },
    { label: '消耗仓库', value: 'depotName', type: 'text' },
    { label: '出库部门', value: 'deptName', type: 'text' },
    { label: '消耗部门', value: 'consumeDeptName', type: 'text' },
    { label: '供应商', value: 'supplierName', type: 'text' },
    { label: '确认人', value: 'confirmUserName', type: 'text' },
    { label: '确认时间', value: 'confirmTime', type: 'text' },
    {
      label: '来源消耗单',
      value: 'consumeOrderNo',
      type: 'slot',
    },
    {
      label: '业务日期',
      value: 'businessDate',
      type: 'text',
    },
    { label: '备注', value: 'remark', type: 'popover' },
  ],
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    customizeUserVO: {},
    queryMap: {
      orderId: {
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: props.id,
      },
    },
    queryParams: {},
  },
})
const fullInfo = ref({
  btnList: [],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: undefined,
  data: [],
  summaryFieldList: [
    { value: 'qty', formatter: proxy.$fn.qtyFormatter },
    { value: 'sumAmount', formatter: proxy.$fn.TripartiteMethod },
  ],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '查看调价单',
        event: toPriceDetailLink,
        show: true,
        ifRender: function (row) {
          return row.adjustPriceOrderDetailVOS
        },
      },
      {
        label: '个体详情',
        event: showDetail,
        show: true,
      },
    ],
  },
})
const affairTableInfo = ref({
  api: orderAffairAll,
  query: {
    orderType: 77,
    orderId: props.id,
    sort: 'operate_time desc',
  },
})
const consignOrderInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  query: {
    consignOrderId: props.id,
  },
  fieldList: [
    { label: '寄售消耗确认表单号', value: 'consignOrderNo' },
    { label: '单据类型', value: 'orderTypeValue' },
    {
      label: '关联生成单据表单号',
      value: 'relationOrderNo',
      type: 'slot',
    },
  ],
})
const revokeInfo = ref({
  visible: false,
  orderId: '',
  ids: '',
})
const individualInfo = ref({
  visible: false,
  query: {},
  info: {},
})
const modifyInfo = ref({
  visible: false,
  title: '',
  orderId: '',
})
const purchOrderDetailInfo = ref({
  visible: false,
  orderId: '',
})
const saleOrderDetailInfo = ref({
  visible: false,
  id: '',
})
const deliverDetailInfo = ref({
  visible: false,
  id: '',
})
const receivingDetailInfo = ref({
  visible: false,
  id: '',
})
const transferOrderDetailInfo = ref({
  visible: false,
  data: '',
})
const consumeDetailInfo = ref({
  visible: false,
  id: '',
})
const adjustPriceDetailInfo = ref({
  visible: false,
  btLoading: false,
  width: '45%',
  btList: [
    {
      label: '关闭',
      event: close,
      show: true,
    },
  ],
  tableInfo: {
    data: [],
    fieldList: [
      {
        label: '调价单号',
        value: 'orderNo',
      },
      {
        label: '调整前单价',
        value: 'beforePrice',
      },
      {
        label: '调整后单价',
        value: 'afterPrice',
      },
    ],
  },
})
const dialogTableInfo = ref({
  data: [],
  fieldList: [{ label: '移库路径', value: 'name' }],
})
const dialogInfoPath = ref({
  title: '移库路径',
  visible: false,
  type: '',
  width: '670px',
  btLoading: false,
  row: {},
  path: {},
  btList: [
    { label: '关闭', type: '', icon: '', event: closePath, show: true },
    {
      label: '保存',
      type: 'primary',
      icon: '',
      disabled: true,
      event: confirmByPathFun,
      saveLoading: false,
      show: true,
    },
  ],
})
// 过滤相关配置
const filterOrderInfo = ref({
  keyword: '',
  query: {
    queryMap: {
      id: {
        queryType: 'IN',
        queryCategory: 'AND',
        value: '',
      },
    },
    queryParams: {},
    customizeUserVO: {},
  },
  list: [{ slot: true, value: 'button' }],
})
// 表格相关
const tableOrderInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  copyData: [],
  fieldList: [],
})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件visible
  },
})
const canUpdate = computed(() => {
  return (
    proxy.$permission('consignConsumeConfirmOrder:update') &&
    infoList.value.data.status === 1
  )
})
const canSubmit = computed(() => {
  return (
    proxy.$permission('consignConsumeConfirmOrder:submit') &&
    infoList.value.data.status === 1
  )
})
const canRatify = computed(() => {
  return (
    proxy.$permission('consignConsumeConfirmOrder:ratify') &&
    infoList.value.data.status === 2
  )
})
const canReview = computed(() => {
  return (
    proxy.$permission('consignConsumeConfirmOrder:review') &&
    infoList.value.data.status === 3
  )
})
const canConfirm = computed(() => {
  return (
    proxy.$permission('consignConsumeConfirmOrder:confirm') &&
    infoList.value.data.status === 4
  )
})
const canConfirmCancel = computed(() => {
  return (
    proxy.$permission('consignConsumeConfirmOrder:confirmCancel') &&
    infoList.value.data.status === 5
  )
})
const canReject = computed(() => {
  return (
    proxy.$permission('consignConsumeConfirmOrder:reject') &&
    infoList.value.data.status === 2
  )
})
const canRevoke = computed(() => {
  return (
    (proxy.$permission('consignConsumeConfirmOrder:revoke') &&
      infoList.value.data.status === 2) ||
    infoList.value.data.status === 3 ||
    infoList.value.data.status === 4
  )
})
const canGoBack = computed(() => {
  return (
    proxy.$permission('consignConsumeConfirmOrder:goBack') &&
    infoList.value.data.status === 3
  )
})
const canCancel = computed(() => {
  return (
    proxy.$permission('consignConsumeConfirmOrder:cancel') &&
    infoList.value.data.status === 4
  )
})
const canExport = computed(() => {
  return proxy.$permission('consignConsumeConfirmOrder:exportDetail')
})
const canPrint = computed(() => {
  return proxy.$permission('consignConsumeConfirmOrder:printDetail')
})
watch(
  () => dialogInfoPath.value.visible,
  (val) => {
    if (!val) {
      dialogInfoPath.value.btList[1].disabled = true
    }
  }
)
watch(
  () => individualInfo.value.visible,
  (value) => {
    if (!value) {
      getMain()
    }
  }
)
watch(
  () => tableInfo.value.data,
  (value) => {
    if (value) {
      const ids = []
      value.forEach((item) => {
        if (item.groupConcatIds) {
          //未定义表示没有分组条件
          ids.push(item.groupConcatIds)
        } else {
          ids.push('' + item.id)
        }
      })
      filterOrderInfo.value.query.queryMap.id.value = ids.join(',')
      getOrderList()
    }
  }
)

setData()
setOrderData()
function setData() {
  let obj = {
    sumAmount: {
      label: '总金额',
      value: 'sumAmount',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      hidden: true,
    },
    consumePrice: {
      label: '消耗单价',
      value: 'consumePrice',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      hidden: true,
    },
    orderNo: {
      label: '单号',
      value: 'orderNo',
      componentType: 'input',
      show: false,
      type: 'column',
      hidden: true,
    },
    defaultSupplierPrice: {
      label: '默认供应商价格',
      value: 'defaultSupplierPrice',
      type: 'column',
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    stockPrice: {
      label: '入库单价',
      value: 'stockPrice',
      componentType: 'price',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      show: false,
      hidden: true,
    },
    price: {
      label: '结算单价',
      value: 'price',
      componentType: 'price',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      show: false,
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  getCustomize()
}
function setOrderData() {
  let obj = {
    orderNo: {
      label: '单号',
      value: 'orderNo',
      componentType: 'input',
      show: false,
      type: 'column',
      hidden: true,
    },
    consumeOrderNo: {
      label: '单号',
      value: 'consumeOrderNo',
      componentType: 'input',
      type: 'slot',
      show: false,
      hidden: true,
    },
    price: {
      label: '单价',
      value: 'price',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      show: false,
      hidden: true,
    },
    sumAmount: {
      label: '总金额',
      value: 'sumAmount',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      show: false,
      hidden: true,
    },
    defaultSupplierPrice: {
      label: '默认供应商价格',
      value: 'defaultSupplierPrice',
      type: 'column',
      permission: !proxy.$permission('consignConsumeConfirmOrder:price'),
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    invalidQty: {
      label: '撤回数量',
      value: 'invalidQty',
      type: 'column',
      componentType: 'number',
      formatter: utils.qtyFormatter,
      show: false,
      hidden: true,
    },
  }
  tableOrderInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...obj,
  })
  getOrderCustomize()
}
function getOrderNoList(val) {
  if (!val) return []
  if (val.includes(',')) {
    return val.split(',')
  }
  return [val]
}
function close() {
  adjustPriceDetailInfo.value.tableInfo.data = []
  adjustPriceDetailInfo.value.visible = false
}
function toPriceDetailLink(row) {
  if (row.adjustPriceOrderDetailVOS) {
    adjustPriceDetailInfo.value.tableInfo.data = row.adjustPriceOrderDetailVOS
    adjustPriceDetailInfo.value.visible = true
  }
}
function showDetailLink(row) {
  if (row.orderType === 0) {
    //采购单
    purchOrderDetailInfo.value.orderId = row.relationOrderId
    purchOrderDetailInfo.value.visible = true
  } else if (row.orderType === 17) {
    //销售单
    saleOrderDetailInfo.value.id = row.relationOrderId
    saleOrderDetailInfo.value.visible = true
  } else if (row.orderType === 18) {
    //发货单
    deliverDetailInfo.value.id = row.relationOrderId
    deliverDetailInfo.value.visible = true
  } else if (row.orderType === 15) {
    //收货单
    receivingDetailInfo.value.id = row.relationOrderId
    receivingDetailInfo.value.visible = true
  } else if (row.orderType === 2) {
    //移库单
    transferOrderDetailInfo.value.data = { id: row.relationOrderId }
    transferOrderDetailInfo.value.visible = true
  } else if (row.orderType === 25) {
    //消耗单
    consumeDetailInfo.value.id = row.relationOrderId
    consumeDetailInfo.value.visible = true
  }
}
function individualDetailsCallBack(id) {
  if (id) {
    consumeDetailInfo.value.id = id
    consumeDetailInfo.value.visible = true
  }
}
//提交
function submit() {
  const ids = [infoList.value.data.id]
  submitConsignConsumeConfirmOrder(ids).then((resp) => {
    if (resp && resp.success) {
      returnModify()
    }
  })
}
//驳回
function reject() {
  const ids = [infoList.value.data.id]
  rejectConsignConsumeConfirmOrder(ids).then((resp) => {
    if (resp && resp.success) {
      returnModify()
    }
  })
}
function goBack() {
  //退回
  const ids = [infoList.value.data.id]
  backConsignConsumeConfirmOrder(ids).then((resp) => {
    if (resp && resp.success) {
      returnModify()
    }
  })
}
function handleDialogEvent(event, data) {
  if (event === 'tableClickRow') {
    dialogInfoPath.value.btList[1].disabled = false
    dialogInfoPath.value.path = data.value
  }
}
function confirm() {
  getConfirmPath(infoList.value.data.id).then((res) => {
    if (!res.data.length) {
      //确认
      confirmConsignConsumeConfirmOrder(infoList.value.data.id).then((resp) => {
        if (resp && resp.success) {
          returnModify()
        }
      })
    }
    if (res.data.length === 1) {
      _confirmByPath({ id: infoList.value.data.id, path: res.data[0].value })
    }
    if (res.data.length > 1) {
      dialogTableInfo.value.data = res.data
      dialogInfoPath.value.visible = true
    }
  })
}
function confirmCancel() {
  //取消确认
  confirmCancelConsignConsumeConfirmOrder(infoList.value.data.id).then(
    (resp) => {
      if (resp && resp.success) {
        returnModify()
        getConsignOrderList()
      }
    }
  )
}

function confirmByPathFun() {
  confirmByPath({
    id: infoList.value.data.id,
    path: dialogInfoPath.value.path,
  }).then((resp) => {
    if (resp && resp.success) {
      dialogInfoPath.value.visible = false
      returnModify()
    }
  })
}
function _confirmByPath(params) {
  confirmByPath(params).then((resp) => {
    if (resp && resp.success) {
      returnModify()
    }
  })
}
function closePath() {
  dialogInfoPath.value.visible = false
}
//审核
function ratify() {
  const ids = [infoList.value.data.id]
  ratifyConsignConsumeConfirmOrder(ids).then((resp) => {
    if (resp && resp.success) {
      returnModify()
    }
  })
}
function review() {
  //复核

  const ids = [infoList.value.data.id]
  reviewConsignConsumeConfirmOrder(ids).then((resp) => {
    if (resp && resp.success) {
      returnModify()
    }
  })
}
//撤回
function revoke() {
  const ids = [infoList.value.data.id]
  revokeConsignConsumeConfirmOrder(ids).then((resp) => {
    if (resp && resp.success) {
      returnModify()
    }
  })
}
//取消
function cancel() {
  const ids = [infoList.value.data.id]
  cancelConsignConsumeConfirmOrder(ids).then((resp) => {
    if (resp && resp.success) {
      returnModify()
    }
  })
}
//更新
function update() {
  modifyInfo.value.title = '修改寄售消耗确认单'
  modifyInfo.value.orderId = infoList.value.data.id
  modifyInfo.value.visible = true
}
function returnBack() {
  emits('returnBack')
}
function showDetail(row) {
  let ids
  if (row.groupConcatIds) {
    //未定义表示没有分组条件
    ids = row.groupConcatIds
  } else {
    ids = '' + row.id
  }
  individualInfo.value.info = row
  individualInfo.value.query = {
    id: {
      queryType: 'IN',
      queryCategory: 'AND',
      value: ids,
    },
  }
  individualInfo.value.visible = true
}
function returnModify() {
  getMain()
  getList()
}
function getMain() {
  getConsignConsumeConfirmOrderById(props.id).then((resp) => {
    if (resp && resp.success) {
      infoList.value.data = resp.data
      if (!resp.data) {
        showDialog.value = false
        emits('returnBack')
      }
      getList()
      getConsignOrderList()
    }
  })
}
function getConsignOrderList() {
  getConsignOrderAll(consignOrderInfo.value.query).then((resp) => {
    if (resp && resp.success) {
      consignOrderInfo.value.data = resp.data
    }
  })
}
function getList() {
  getConsignConsumeConfirmDetailsCustomizeListAll(filterInfo.value.query).then(
    (resp) => {
      if (resp && resp.success) {
        tableInfo.value.data = resp.data
      }
    }
  )
}
function getOrderList() {
  getConsignConsumeConfirmDetailsCustomizeListAll(
    filterOrderInfo.value.query
  ).then((resp) => {
    if (resp && resp.success) {
      tableOrderInfo.value.data = resp.data
    }
  })
}
const templateArr = ref([])
const popoverVisible = ref(false)
//打印
function print() {
  selectByCodeAndDept({
    tempCode: 'consignConsumeConfirmDetail',
    deptId: infoList.value.data.deptId,
  }).then((res) => {
    if (res && res.success) {
      templateArr.value = res.data
      popoverVisible.value = true
      // 一条数据则直接打印
      if (templateArr.value.length === 1) {
        proxy.$refs.popoverShow.hidePopover()
        doPrint({
          name: templateArr.value[0].ureportName,
          params: {
            orderId: infoList.value.data.id,
            orderNo: infoList.value.data.orderNo,
          },
          callback: printCallback,
          printCount: true,
          data: {},
        })
      }
    }
  })
  /*  fatchTemplateByCode({
    resourceCode: 'consignConsumeConfirmOrder:print',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPirntData(temp)
    }
  }) */
}
function printCallback() {
  try {
    // 打印成功增加打印次数
    updatePrintCount({
      orderNo: infoList.value.data.orderNo,
      orderId: infoList.value.data.id,
      tempCode: 'consignConsumeConfirmDetail',
      orderType: 77,
    })
  } catch (error) {}
}
function tempClick(item) {
  doPrint({
    name: item.ureportName,
    params: {
      orderId: infoList.value.data.id,
      orderNo: infoList.value.data.orderNo,
    },
    callback: printCallback,
    printCount: true,
    data: {},
  })
  popoverVisible.value = false
}
function getPirntData(temp) {
  let params = {
    tableKey:
      'consignConsumeConfirmOrderDetail-com.slip.spd.entity.vo.stock.ConsignConsumeConfirmOrderDetailVO',
    orderIds: [props.id],
  }
  printDetailConsignConsumeConfirmOrder(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(
        temp,
        newData,
        !proxy.$permission('consignConsumeConfirmOrder:price')
      )
    }
  })
}
//导出excel
function exportExcel() {
  excelDetailConsignConsumeConfirmOrder(filterInfo.value.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function checkSummaryFlag(val) {
  toSummaryFlag.value = val
  infoList.value.fieldList.forEach((item) => {
    if (item.value === 'consumeOrderNo') {
      item.need = val ? '' : 'no'
    }
  })
}
function getCustomize() {
  let _tableInfo = tableInfo.value
  let param = {
    tableKey:
      'consignConsumeConfirmOrderDetail-com.slip.spd.entity.vo.stock.ConsignConsumeConfirmOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.value.query.customizeUserVO = res.data
      _tableInfo.fieldList = proxy.$fn.setFieldList(
        _tableInfo.fieldList,
        data.detail
      )
      getMain()
    }
  })
}
function getOrderCustomize() {
  let param = {
    tableKey:
      'consignConsumeConfirmOrderDetailModifySingle-com.slip.spd.entity.vo.stock.ConsignConsumeConfirmOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterOrderInfo.value.query.customizeUserVO = res.data
      tableOrderInfo.value.fieldList = proxy.$fn.setFieldList(
        tableOrderInfo.value.fieldList,
        data.detail
      )
      tableOrderInfo.value.api = getConsignConsumeConfirmDetailsCustomizeListAll
    }
  })
}
</script>

<style lang="scss" scoped>
.model-panel-title.btnList {
  display: flex;
  align-items: center;
  .model-tips {
    display: flex;
    align-items: center;
    margin-left: 10px;
    color: #909399;
    .model-tips-nearly,
    .model-tips-expire {
      display: block;
      margin-left: 5px;
      width: 17px;
      height: 17px;
    }
    .model-tips-nearly {
      background: #fceacb;
    }
    .model-tips-expire {
      background: #f5e4e4;
    }
  }
}
</style>
